//    Copyright (C) Kherty.  All rights reserved.
using System.Collections.Generic;
using System.Globalization;
using System.Windows;
using System.Windows.Media;

namespace OpenLS.Spreadsheet
{
    class FontMetrics
    {
        readonly Dictionary<FormattingRecord, double> RecordToHeight = new Dictionary<FormattingRecord, double>();
        FormattingRecord last = null;
        double lastResult;
        public double GetHeightForFormattingRecord(FormattingRecord r, Workbook workbook)
        {
            if (r == last)
                return lastResult;
            if (r.FontSize == 0)
                return 0;
            double result;
            if (RecordToHeight.TryGetValue(r, out result))
                return result;
            FontFamily f = r.FontFamily;
            if (f == null)
                f = workbook.FontFamily;
            FormattedText ft = new FormattedText("AaQqKkJjMm", CultureInfo.CurrentCulture, FlowDirection.LeftToRight, SpreadsheetFont.CreateTypeface(f,
                r.FontStyle, r.FontWeight), r.FontSize, Brushes.Black);
            result = ft.Height;
            RecordToHeight.Add(r, result);
            lastResult = result;
            last = r;
            return result;
        }
    }
}
